package de.hwrBerlin.dataMining.view.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import de.hwrBerlin.dataMining.constants.ConfigConstants;
import de.hwrBerlin.dataMining.constants.Images;
import de.hwrBerlin.dataMining.constants.UIConstants;
import de.hwrBerlin.dataMining.controller.Controller;

public class AddAttributeDialog extends Dialog {

	private int _returnValue = SWT.CANCEL;

	public AddAttributeDialog(Shell pParent) {
		super(pParent);
	}

	public AddAttributeDialog(Shell pParent, int pStyle) {
		super(pParent, pStyle);
	}

	public int open(final Controller pController) {

		Shell parent = getParent();
		Display display = parent.getDisplay();

		final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.CLOSE);
		shell.setText(UIConstants.ADD_COLUMN_TOOL_TIP);
		shell.setImages(Images.LOGO_ICONS);
		shell.setLayout(new GridLayout(2, true));

		if (pController.getModel().getTable().getAttributCount() > ConfigConstants.MAX_ATTRIBUTES - 1) {
			Label warning = new Label(shell, SWT.NONE);
			warning.setForeground(new Color(display, 255, 0, 0));
			warning.setText(UIConstants.TOO_MUCH_ATTRIBUTES);
			warning.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

			Label seperator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
			seperator.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		}

		Label atributeNameLabel = new Label(shell, SWT.NONE);
		atributeNameLabel.setText(UIConstants.ATTRIBUT_NAME + ": ");

		final Text attributeName = new Text(shell, SWT.BORDER);
		attributeName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Label defaultAttributeValueLabel = new Label(shell, SWT.NONE);
		defaultAttributeValueLabel.setText(UIConstants.ATTRIBUT_DEFAULT_VALUE + ": ");
        if(pController.getModel().getTable().getRowCount() <= 1){
            defaultAttributeValueLabel.setEnabled(false);
        }

		final Text defaultAttributeValue = new Text(shell, SWT.BORDER);
		defaultAttributeValue.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		if(pController.getModel().getTable().getRowCount() <= 1){
		    defaultAttributeValue.setEnabled(false);
		}

		Button okButton = new Button(shell, SWT.PUSH);
		okButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		okButton.setText(UIConstants.OK);
		okButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent pE) {
				if(pController.addAttribute(attributeName.getText(), defaultAttributeValue.getText())){
				    _returnValue = SWT.OK;
				    shell.close();
				}
			}
		});

		Button cancelButton = new Button(shell, SWT.PUSH);
		cancelButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		cancelButton.setText(UIConstants.CANCEL);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent pE) {
				_returnValue = SWT.CANCEL;
				shell.close();
			}
		});

		shell.pack();
		shell.open();
		shell.setLocation(new Point(parent.getLocation().x + 20, parent.getLocation().y + 20));
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return _returnValue;
	}

}
